package org.origin.centre.aspect;

import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.origin.centre.support.utils.ServletUtil;
import org.origin.centre.support.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * 切面处理
 *
 * @author ferret
 * @version 2024-05-08
 */
@Aspect
public class AspectInterceptor {
    private final static Logger logger = LoggerFactory.getLogger(AspectInterceptor.class);

    @Around(value = "execution(public * org.origin.*.*.controller.*.*(..))&&" +
            "!execution(public * org.origin.*.*.controller.*.get(..))&&" +
            "!within(org.origin.*.*.*.SysLogsController))")
    public Object doRequestLogsAround(ProceedingJoinPoint point) throws Throwable {
        Date startTime = new Date();
        try {
            Object[] args = point.getArgs();
            Signature signature = point.getSignature();
            HttpServletRequest request = ServletUtil.getRequest();
            logger.info("请求方法：{}.{}", signature.getDeclaringTypeName(), signature.getName());
            logger.info("访问地址：{}", request.getRequestURL().toString());
            logger.info("请求地址：{}", request.getRemoteAddr());
            logger.info("请求方式：{}", request.getMethod());
            logger.info("请求参数：{}", args != null ? JsonUtil.toJsonString(args) : null);
            Object proceed = point.proceed();
            logger.info("请求耗时：{}", new Date().getTime() - startTime.getTime());
            logger.info("请求结果：{}", proceed != null ? JsonUtil.toJsonString(proceed) : null);
            return proceed;
        } catch (Exception e) {
            logger.info("请求耗时：{}", new Date().getTime() - startTime.getTime());
            logger.info("请求结果：{}", e.getMessage());
            throw e;
        }
    }

    // @Around(value = "execution(public * org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(..))")
    public Object doLoginLogsAround(ProceedingJoinPoint point) throws Throwable {
        Date startTime = new Date();
        try {
            Object[] args = point.getArgs();
            Signature signature = point.getSignature();
            HttpServletRequest request = ServletUtil.getRequest();
            logger.info("请求方法：{}.{}", signature.getDeclaringTypeName(), signature.getName());
            logger.info("访问地址：{}", request.getRequestURL().toString());
            logger.info("请求地址：{}", request.getRemoteAddr());
            logger.info("请求方式：{}", request.getMethod());
            logger.info("请求参数：{}", args != null ? JsonUtil.toJsonString(args) : null);
            Object proceed = point.proceed();
            logger.info("请求耗时：{}", new Date().getTime() - startTime.getTime());
            logger.info("请求结果：{}", proceed != null ? JsonUtil.toJsonString(proceed) : null);
            return proceed;
        } catch (Exception e) {
            logger.info("请求耗时：{}", new Date().getTime() - startTime.getTime());
            logger.info("请求结果：{}", e.getMessage());
            throw e;
        }
    }
}
